﻿using Newtonsoft.Json;
using Newtonsoft.Json.Linq;
using P2P.Main.Model;
using P2P.Main.Session;
using System;
using System.Collections.Concurrent;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace P2P.Main.Handler
{
    public class ActiveNodeListHandler
    {
        private ApiProtocol apiProtocol;

        public ActiveNodeListHandler(ApiProtocol apiProtocol)
        {
            this.apiProtocol = apiProtocol;
        }

        public void ActiveNodeList() 
        {
            string AccountId = (string)apiProtocol.Params["AccountId"];

            ICollection<ActiveNode> activeNodes = ActiveNodeSessionManager.Instance.ActiveNodeDict.Values;

            JObject data = new JObject();
            data.Add("Name", "ActiveNodeList");
            data.Add("Method", "ActiveNodeList");
            data.Add("Version", "1.0");
            data.Add("Params", JObject.FromObject(activeNodes));

            string ack = JsonConvert.SerializeObject(R<JObject>.Ok(data));
            apiProtocol.Socket.SendTo(Encoding.UTF8.GetBytes(ack), apiProtocol.RemoteEndPoint);
        }
    }
}
